package org.sdkj.device.test.tcp.utils;

import java.io.IOException;
import java.io.OutputStream;

/**
 * 数据包发送器
 * 
 * @author lenovo
 * 
 * 
 */
public class MessageSender {
	private OutputStream out_s = null;

	public static MessageSender getInstance(OutputStream out_s) {
		MessageSender msobj = new MessageSender();
		msobj.out_s = out_s;
		return msobj;
	}

	/**
	 * 说谢谢
	 */
	public void sayThank() {
		byte[] bData = new byte[16];
		bData[0] = 30;// 
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}

	/**
	 * 说请放手指
	 */
	public void sayPutFinger() {
		byte[] bData = new byte[16];
		bData[0] = 27;// 
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}
	/**
	 * 说请正确放置手指
	 */
	public void sayPutFingerRight() {
		byte[] bData = new byte[16];
		bData[0] = 26;// 
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}
	/**
	 * 说再放一次
	 */
	public void sayRePutFinger() {
		byte[] bData = new byte[16];
		bData[0] = 23;//
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}

	/**
	 * 说登记成功
	 */
	public void sayBookSuccess() {
		byte[] bData = new byte[16];
		bData[0] = 4;// 
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}

	
	/**
	 * 说验证成功
	 */
	public void sayCheckSuccess() {
		byte[] bData = new byte[16];
		bData[0] = 33;//
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}
	
	/**
	 * 说验证失败
	 */
	public void sayCheckFail() {
		byte[] bData = new byte[16];
		bData[0] = 32;//
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SendPackToClient((byte) 0x0A, bData, 1);
	}
	
	/**
	 * 发送数据包到客户端
	 * 
	 * @param cmd
	 * @param bData
	 * @param len
	 * @return
	 */
	private int SendPackToClient(byte cmd, byte[] bData, int len) {
		int PackSize = len + 8;
		if (PackSize < 24)
			PackSize = 24;

		byte[] PackBuf = new byte[PackSize];
		for (int i = 0; i < PackSize; i++) {
			PackBuf[i] = 0;
		}

		PackBuf[0] = (byte) 0xDD;
		PackBuf[1] = (byte) 0xCC;
		PackBuf[2] = (byte) 0; // bAddress
		PackBuf[3] = (byte) cmd;
		PackBuf[4] = (byte) (len & 0xff);
		PackBuf[5] = (byte) ((len >> 8) & 0xff);
		for (int i = 0; i < len; i++) {
			PackBuf[i + 6] = bData[i];
		}
		int checknum = DevConn.CheckSum(PackBuf, PackSize - 2);
		PackBuf[len + 6] = (byte) (checknum & 0xff);
		PackBuf[len + 7] = (byte) (((checknum & 0xFFFF) >> 8) & 0xff);
		if (out_s != null) {
			try {
				out_s.write(PackBuf);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return PackSize;
	}
}
